package com.atguigu.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/Servlet02BringCookie")
public class Servlet02BringCookie extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();

        // 1、从请求中读取 Cookie 信息
        Cookie[] cookies = request.getCookies();

        // 2、遍历数组
        for (int i = 0; cookies != null && cookies.length > 0 && i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            String name = cookie.getName();
            String value = cookie.getValue();
            writer.write(name + " = " + value + "<br/>");
        }
    }
}
